package com.example.e_comm.util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import java.util.concurrent.TimeUnit;

/** * Redis工具类, 用于缓存数据(获取缓存，设置缓存，删除缓存，判断缓存是否存在，设置缓存时长
 )
 * */@Component
public class RedisUtil {
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;
    /**
     设置缓存     * @param key
     缓存键     * @param value
     缓存值     * @param time
     缓存时长     * @param timeUnit
     缓存时长单位
     * */    public void set(String key, Object value, long time, TimeUnit timeUnit){
        redisTemplate.opsForValue().set(key, value, time, timeUnit);
    }
    /**
     获取缓存     * @param key
     缓存键     * @return
     缓存值
     */    public Object get(String key){
        return redisTemplate.opsForValue().get(key);
    }
    /**
     删除缓存     * @param key
     缓存键
     */    public boolean delete(String key){
        return Boolean.TRUE.equals(redisTemplate.delete(key));
    }
    /**
     判断缓存是否存在     * @param key
     缓存键     * @return
     true/false
     */    public boolean exists(String key){
        return Boolean.TRUE.equals(redisTemplate.hasKey(key));
    }
    /**
     设置缓存时长     * @param key
     缓存键     * @param time
     缓存时长     * @param timeUnit
     缓存时长单位     *
     返回：设置是否成功
     */    public boolean expire(String key, long time, TimeUnit timeUnit){
        return Boolean.TRUE.equals(redisTemplate.expire(key, time, timeUnit));
    }

    public boolean hasKey(String s) {
    }
}
